package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test4 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        //获取Person类对象
        Class cls=Class.forName("reflect.Person");
        //创建对象，调用String参构造方法
        Constructor c=cls.getConstructor(String.class);//获取构造方法对象
        Object obj=cls.newInstance();//创建对象
        Method[]methods=cls.getDeclaredMethods();
        for (Method method:methods){
            if (method.isAnnotationPresent(AutoRunMethod.class)){
                AutoRunMethod arm=method.getAnnotation(AutoRunMethod.class);
                int value=arm.value();
                System.out.println("调用方法:"+method.getName());
                for (int i=0;i<value;i++){
                    method.invoke(obj);
                }
            }else {
                System.out.println(method.getName()+"()没有被标注");
            }
        }
    }
}
